namespace Digitalmes.Application.Mes;

/// <summary>
/// 设备点检详细记录表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "mes")]
public sealed class EquipCheckDocLineService : IDynamicWebApiService 
{
    private readonly SugarRepository<EquipCheckDocLine> _thisRepository;

    public EquipCheckDocLineService(SugarRepository<EquipCheckDocLine> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<EquipCheckDocLineDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<EquipCheckDocLineDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<EquipCheckDocLineDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<EquipCheckDocLineDto>();
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(EquipCheckDocLineDto model)
    {
        return await _thisRepository.UpdateAsync(model.Adapt<EquipCheckDocLine>());
    }

    /// <summary>
    /// 保存点检结果
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPut]
    public async Task<bool> SaveResultAsync(EquipCheckDocLineDto model)
    {
        var item = await _thisRepository.GetByIdAsync(model.Id);
        if (item is null)
        {
            return true;
        }

        item.Result = model.Result;
        return await _thisRepository.AsUpdateable(item).UpdateColumns(s => new { s.Result }).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 保存点检描述
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPut]
    public async Task<bool> SaveRemarkAsync(EquipCheckDocLineDto model)
    {
        var item = await _thisRepository.GetByIdAsync(model.Id);
        if (item is null)
        {
            return true;
        }

        item.Remark = model.Remark;
        return await _thisRepository.AsUpdateable(item).UpdateColumns(s => new { s.Remark }).ExecuteCommandAsync() > 0;
    }
}
